package com.controller;

import javax.servlet.http.HttpServletRequest;

import com.dto.User;
import com.service.UserService;
import com.utils.StaticTest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author HuangSuip
 */
@RequestMapping("/user")
@Controller
public class UserController {

    private final Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private UserService userService;

    @GetMapping("{id}")
    @ResponseBody
    public Object getUser(@PathVariable("id") Integer id) {
        return userService.getUserNameById(id);
    }

    @GetMapping("user")
    @ResponseBody
    public Object insert(
        @RequestParam Integer age,
        @RequestParam String name
    ) {
        User u = new User();
        u.setAge(age);
        u.setName(name);
        userService.insert(u);
        return u;
    }

    @GetMapping("port")
    @ResponseBody
    public Object getPort() {
        StaticTest s = new StaticTest();
        return StaticTest.port;
    }

    @GetMapping("session")
    @ResponseBody
    public Object getSession(HttpServletRequest request){
        System.out.println();request.getSession().isNew();
        return request.getSession().getId();
    }

    @PostMapping("post/{post}")
    @ResponseBody
    public Object postTest(
        @RequestParam("id") Long id,
        @RequestBody User u,
        @PathVariable("post") String post
    ){

        return id + "  " + u.toString() + " " +post;
    }
}
